import { defineConfig } from 'vite'
import path from 'node:path'
import electron from 'vite-plugin-electron/simple'
import vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        WindiCSS(),
        electron({
            main: {
                // Shortcut of `build.lib.entry`.
                entry: 'electron/main.ts',
                vite: {
                    build: {
                        outDir: 'dist',
                        minify: false,
                        terserOptions: {
                            mangle: false,
                        },
                    },
                },
            },
            // preload: {
            // Shortcut of `build.rollupOptions.input`.
            // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
            // input: path.join(__dirname, 'electron/preload.ts'),
            // },
            // Ployfill the Electron and Node.js API for Renderer process.
            // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
            // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
            renderer:
                process.env.NODE_ENV === 'test'
                    ? // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
                      undefined
                    : {},
        }),
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src'),
        },
    },
    build: {
        outDir: 'dist/renderer',
        minify: false,
        // 关闭变量重命名
        terserOptions: {
            mangle: false,
        },
        rollupOptions: {
            output: {
                manualChunks: function manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return 'vendor'
                    }
                    // console.log(id)
                    const path = id.split('src/')[1]
                    if (!path) return null
                    // console.log(path)
                    const splited = path.split('.')
                    splited.pop()
                    // console.log(splited)
                    // return null
                    const withoutExtension = splited.join('.')
                    if (withoutExtension) {
                        return withoutExtension
                    }

                    return null
                },
                chunkFileNames: `[name].js`,
                assetFileNames: 'assets/css/[name].[ext]'
                // entryFileNames: (id) => {
                //     console.log(id)
                //     return `[name]12.js`
                // },
                // preserveModules: true,
                // preserveModulesRoot: 'src', // 指定模块的根目录
                // dir: 'dist/renderer'
            },
        },
    },
})
